package com.binhthuan.catechist.client.content.training;

import com.binhthuan.catechist.client.AbstractContent;
import com.binhthuan.catechist.client.ContentFactory;
import com.binhthuan.catechist.client.ECApplication;
import com.binhthuan.catechist.client.ECIcons;
import com.binhthuan.catechist.client.component.PopupDialog;
import com.binhthuan.catechist.client.data.TraineeDS;
import com.binhthuan.catechist.client.form.AbstractForm;
import com.binhthuan.catechist.client.form.TraineeInformationForm;
import com.binhthuan.catechist.client.form.IForm.UpdateSuccessHandler;
import com.binhthuan.catechist.client.grid.AbstractGrid;
import com.binhthuan.catechist.client.grid.InactiveTraineeGrid;
import com.binhthuan.catechist.client.grid.IGrid.DeleteButtonClickHandler;
import com.binhthuan.catechist.client.grid.IGrid.EditButtonClickHandler;
import com.binhthuan.catechist.client.grid.IGrid.ViewButtonClickHandler;
import com.binhthuan.catechist.client.viewer.PersonViewer;
import com.binhthuan.catechist.client.viewer.TraineeViewer;
import com.binhthuan.catechist.shared.Utilities;
import com.binhthuan.catechist.shared.constant.Parameter;
import com.smartgwt.client.data.Criteria;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.VisibilityMode;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.Dialog;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.grid.events.DataArrivedEvent;
import com.smartgwt.client.widgets.grid.events.DataArrivedHandler;
import com.smartgwt.client.widgets.layout.SectionStack;
import com.smartgwt.client.widgets.layout.SectionStackSection;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.toolbar.ToolStrip;
import com.smartgwt.client.widgets.toolbar.ToolStripButton;

public class TraineeRegisterPage extends AbstractContent {

    public static class Factory implements ContentFactory {

        private static Factory instance;
        private String id;

        private Factory() {
            if (id == null) {
                id = Utilities.generateID(TraineeRegisterPage.class.getName());
            }
        }

        /**
         * @return the instance
         */
        public static Factory getInstance() {
            if (instance == null) {
                instance = new Factory();
            }

            return instance;
        }

        @Override
        public boolean checkCertificated(long securityCode) {
            return true;
        }

        @Override
        public AbstractContent create() {
            TraineeRegisterPage content = new TraineeRegisterPage();
            return content;
        }

        @Override
        public String getID() {
            return id;
        }

        @Override
        public String getIcon() {
            return ECIcons.getIcon("sign-up.png");
        }

        @Override
        public String getTitle() {
            return ECApplication.getConstants().traineeRegister();
        }
    }

    private PopupDialog inputDialog;
    private AbstractGrid reportGrid;
    private AbstractForm inputEditor;
    private Label totalItems;

    @Override
    public Canvas getContentPanel() {
        VLayout mainLayout = new VLayout();
        mainLayout.setWidth100();
        mainLayout.setHeight100();

        SectionStack stack = new SectionStack();
        stack.setWidth100();
        stack.setHeight100();
        stack.setAnimateSections(true);
        stack.setShowEdges(true);
        stack.setVisibilityMode(VisibilityMode.MULTIPLE);

        totalItems = new Label();
        totalItems.setAlign(Alignment.RIGHT);

        ToolStripButton addButton = new ToolStripButton();
        addButton.setIcon(ECIcons.getIcon("add.png"));
        addButton.setPrompt(ECApplication.getMessages().addItem(ECApplication.getConstants().trainee()));
        addButton.addClickHandler(new ClickHandler() {

            @Override
            public void onClick(ClickEvent event) {
                ensureInputEditor().editNewRecord();
                ensureInputDialog(ECApplication.getMessages().addItem(ECApplication.getConstants().trainee())).show();
            }
        });

        ToolStripButton deleteButton = new ToolStripButton();
        deleteButton.setIcon(ECIcons.getIcon("delete.png"));
        deleteButton.setPrompt(ECApplication.getMessages().deleteItem(ECApplication.getConstants().trainee()));
        deleteButton.addClickHandler(new ClickHandler() {

            @Override
            public void onClick(ClickEvent event) {
                reportGrid.removeSelectedItems(true);
            }
        });

        ToolStrip toolStrip = new ToolStrip();
        toolStrip.addButton(addButton);
        toolStrip.addButton(deleteButton);

        reportGrid = new InactiveTraineeGrid(TraineeDS.getInstance());
        reportGrid.setShowActionsField(true);
        //reportGrid.setShowRollOverCanvas(true);
        reportGrid.setShowDetailButton(true);
        reportGrid.setShowEditButton(true);
        reportGrid.setShowDeleteButton(true);
        reportGrid.addViewButtonHandler(new ViewButtonClickHandler() {

            @Override
            public void onClick(ListGridRecord selectedRecord) {
                PersonViewer viewer = new TraineeViewer(TraineeDS.getInstance());
                viewer.fetchData(new Criteria(Parameter.ID, selectedRecord.getAttribute(Parameter.ID)));
                PopupDialog dialog = new PopupDialog(true);
                dialog.addItem(viewer.createCanvas());
                dialog.show();
            }
        });
        reportGrid.addEditButtonHandler(new EditButtonClickHandler() {

            @Override
            public void onClick(ListGridRecord selectedRecord) {
                ensureInputEditor().editRecord(selectedRecord);
                ensureInputDialog(ECApplication.getMessages().updateItem(ECApplication.getConstants().trainee())).show();
            }
        });
        reportGrid.addDeleteButtonHandler(new DeleteButtonClickHandler() {

            @Override
            public void onClick(ListGridRecord selectedRecord) {
                reportGrid.selectRecord(selectedRecord);
                reportGrid.removeSelectedItems(true);
            }
        });

        reportGrid.addDataArrivedHandler(new DataArrivedHandler() {

            @Override
            public void onDataArrived(DataArrivedEvent event) {
                totalItems.setContents(ECApplication.getMessages().numberOfResult(reportGrid.getTotalRows()));
                reportGrid.deselectAllRecords();
            }
        });
        SectionStackSection reportSection = new SectionStackSection(ECApplication.getConstants().trainee());
        reportSection.setCanCollapse(false);
        reportSection.setExpanded(true);
        reportSection.setItems(reportGrid);
        reportSection.setControls(totalItems, addButton, deleteButton);

        stack.setSections(reportSection);
        mainLayout.setMembers(stack);

        return mainLayout;
    }

    @Override
    protected String getHelpContent() {
        return "This is help content for trainee register page";
    }

    public Dialog ensureInputDialog(String title) {
        if (inputDialog == null) {
            inputDialog = new PopupDialog(title);
            inputDialog.addItem(ensureInputEditor());
        } else {
            inputDialog.setTitle(title);
        }
        return inputDialog;
    }

    /**
     * 
     */
    private AbstractForm ensureInputEditor() {
        if (inputEditor == null) {
            inputEditor = new TraineeInformationForm(TraineeDS.getInstance());
            inputEditor.addUpdateSuccessHandler(new UpdateSuccessHandler() {

                @Override
                public void onSuccess() {
                    inputDialog.closeClick();
                }
            });
        }

        return inputEditor;
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * com.binhthuan.catechist.client.AbstractContent#getPrintPreviewPanel()
     */
    @Override
    protected Canvas getPrintPreviewPanel() {
        return reportGrid;
    }
}
